package com.digitalchina.lxnsy.activity.ecardConsult;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import androidx.recyclerview.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.digitalchina.lxnsy.R;
import com.digitalchina.lxnsy.base.BaseV4Fragment;
import com.digitalchina.lxnsy.constant.URL;
import com.digitalchina.lxnsy.entity.EcardConsultVO;
import com.digitalchina.lxnsy.interfaces.ICallBackPos;
import com.digitalchina.lxnsy.toolkit.FastJsonUtil;
import com.digitalchina.lxnsy.toolkit.OkHttpUtil;
import com.digitalchina.lxnsy.toolkit.Utils;
import com.digitalchina.lxnsy.xml.UserXML;
import com.jude.easyrecyclerview.EasyRecyclerView;
import com.jude.easyrecyclerview.adapter.BaseViewHolder;
import com.jude.easyrecyclerview.adapter.RecyclerArrayAdapter;
import com.lzy.okhttputils.model.HttpParams;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

import static android.app.Activity.RESULT_OK;

/**
 * ================================================
 * 项目名称：BigData
 * 类描述：
 * 包名：com.digitalchina.bigdata.fragment
 * 创建人：郑皓
 * 创建时间：2018/8/8 15:31
 * 修改备注：
 * ================================================
 */
public class EcardConsultFragment extends BaseV4Fragment implements RecyclerArrayAdapter.OnLoadMoreListener, SwipeRefreshLayout.OnRefreshListener, ICallBackPos {


    @Bind(R.id.recycler_view)
    EasyRecyclerView recyclerView;

    private RecyclerArrayAdapter adapter;
    private int page = 1;
    private int limit = 10;
    private String type = "";

    private List<EcardConsultVO> voList = new ArrayList<>();

    @Override
    public View setRootView() {
        return inflate(R.layout.fragment_collect);
    }

    @Override
    public void initMember() {

        type = getArguments().getString("type");

        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));

        recyclerView.addItemDecoration(Utils.setDividerDecoration(getActivity(), 10.0f, 0.0f, 0.0f));

        recyclerView.setAdapterWithProgress(adapter = new RecyclerArrayAdapter(getActivity()) {
            @Override
            public BaseViewHolder OnCreateViewHolder(ViewGroup parent, int viewType) {
                return new EcardConsultHolder(parent, EcardConsultFragment.this,type);
            }
        });

        onRefresh();

    }

    @Override
    public void addListener() {
        recyclerView.setRefreshListener(this);

        adapter.setMore(R.layout.view_more, this);

        adapter.setNoMore(R.layout.view_nomore);

        adapter.setError(R.layout.view_error, new RecyclerArrayAdapter.OnErrorListener() {
            @Override
            public void onErrorShow() {
                adapter.resumeMore();
            }

            @Override
            public void onErrorClick() {
                adapter.resumeMore();
            }
        });

    }

    @Override
    public void setHandler() {

        mHandler = new Handler(new Handler.Callback() {
            @Override
            public boolean handleMessage(Message message) {
                if (recyclerView == null) {
                    return false;
                }
                if (recyclerView.getSwipeToRefresh().isRefreshing())
                    recyclerView.setRefreshing(false);

                switch (message.what) {
                    case MSG_S_0:
                        callBack(message.obj);
                        break;
                    case MSG_F_0:
                        if (adapter.getAllData().size() > 0)
                            adapter.pauseMore();
                        else
                            recyclerView.showEmpty();
                        showToast(message.obj.toString());
                        break;
                }
                return false;
            }
        });
    }

    @Override
    public void callBack(Object object) {

        voList = FastJsonUtil.getListBean(object.toString(), "expertConsultList", EcardConsultVO.class);

        if (voList == null)
            return;

        if (page == 1) { // 刷新
            adapter.clear();
        }

        adapter.addAll(voList);

        if (voList.size() < limit) {
            adapter.stopMore();
        }
    }

    @Override
    public void rightOnClick() {

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @Override
    public void onRefresh() {
        page = 1;
        getData();
    }

    @Override
    public void onLoadMore() {
        if (voList != null && voList.size() >= limit) {
            page++;
            getData();
        }
    }

    @Override
    public void clickOk(int pos) {
        EcardConsultVO vo = (EcardConsultVO) adapter.getAllData().get(pos);
        Intent intent = new Intent(getActivity(), EcardReplyConsultActivity.class);
        intent.putExtra("id", vo.getId());
        startActivityForResult(intent, 99);

    }


    public void getData() {
        HttpParams params = new HttpParams();
        params.put("userAccId", UserXML.getUserId(getActivity()));
        params.put("page", String.valueOf(page));
        params.put("limit", String.valueOf(limit));
        params.put("type", type);

        OkHttpUtil.post(getActivity(), URL.URL_GET_EXPERT_CONSULT_REPLY_LIST, "", params, mHandler,
                MSG_S_0, MSG_F_0);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 99 && resultCode == RESULT_OK) {
            onRefresh();
        }
    }
}
